package com.shitou.shitouproduct.controller;


import com.shitou.shitoucommons.annotation.CheckUserLogin;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitouproduct.entity.CategoryEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shitou.shitouproduct.service.CategoryService;
import com.shitou.shitoucommons.utils.RespBean;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author 石头人
 * @email 3076767823@qq.com
 * @date 2023-03-04 18:03:45
 */
@RestController
@RequestMapping("shitouproduct/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return RespBean.success().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{categoryId}")
    public RespBean info(@PathVariable("categoryId") Long categoryId){
        CategoryEntity category = categoryService.getById(categoryId);

        return RespBean.success().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody CategoryEntity category){
        System.out.println(category);
        categoryService.save(category);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody CategoryEntity category){
        System.out.println(category);
        categoryService.updateById(category);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] categoryIds){
        for (Long categoryId : categoryIds) {
            System.out.println(categoryId);
        }

        categoryService.removeByIds(Arrays.asList(categoryIds));

        return RespBean.success();
    }



    /**
     * 获得分类的树结构
     * @return RespBean
     */
    @ApiOperation(value = "获得分类的树形结构")
    @CheckUserLogin(value = false)
    @GetMapping("/treeList")
    public RespBean treeList(){
        return categoryService.queryTreeList();
    }


    /**
     * 获取分类信息
     */
    @ApiOperation(value = "获取分类信息, 需要登录校验")
    @CheckUserLogin(value = false)
    @PostMapping("/info")
    public RespBean infos(@RequestParam Long categoryId){
        return categoryService.info(categoryId);
    }

}
